package com.north.light.libpicselect.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.north.light.libpicselect.PicSelParamsBuilder;
import com.north.light.libpicselect.bean.LibMediaType;
import com.north.light.libpicselect.bean.LibPicInfo;
import com.north.light.libpicselect.bean.LibPicMediaExtraInfo;
import com.north.light.libpicselect.callback.LibPicSelMediaInfo;
import com.north.light.libpicselect.constant.LibPicConstant;

import java.io.Serializable;
import java.util.List;

/**
 * FileName: LibMediaSelectUtils
 * Author: lzt
 * Date: 2022/7/2 16:45
 * 媒体检查工具类
 */
public class LibMediaSelectUtils implements Serializable {


    private static final String TAG = LibMediaSelectUtils.class.getSimpleName();

    /**
     * 校验选择条件
     */
    public static boolean checkCondition(LibPicInfo selInfo, List<LibPicInfo> selPicInfo,
                                         PicSelParamsBuilder builder, TipsListener tipsListener) {
        if (builder == null) {
            if (tipsListener != null) {
                tipsListener.message("参数错误");
            }
            return false;
        }
        //视频选择数量校验--------------------------------
        if (selInfo.getSource() == LibMediaType.VIDEO.getSource()) {

            int videoCountLimit = builder.getVideoCountLimit();
            int localSelVideoCount = 0;
            for (LibPicInfo selCache : selPicInfo) {
                if (selCache.getSource() == LibMediaType.VIDEO.getSource()) {
                    localSelVideoCount = localSelVideoCount + 1;
                }
            }
            if (localSelVideoCount >= videoCountLimit) {
                //选择的视频已经超过了
                if (tipsListener != null) {
                    tipsListener.message("最多只能选择" + videoCountLimit + "个视频");
                }
                return false;
            }
        }
        //选择类型校验--------------------------------
        int onlySelType = builder.getOnlySelOneType();
        if (onlySelType == 0) {
            //没有限制选择类型
            return true;
        }
        //先获取已经选中数据中的类型--------------------------------
        if (selPicInfo == null || selPicInfo.size() == 0) {
            //没有选中，直接对比默认的校对参数
            int mediaType = selInfo.getSource();
            if (onlySelType == 100) {
                //第一个，没要求
                return true;
            }
            boolean compareRes = (onlySelType == mediaType);
            compareAndShowTips(onlySelType, mediaType, tipsListener);
            return compareRes;
        }
        //已选数据--判断是否相对--------------------------------
        int orgSource = selPicInfo.get(0).getSource();
        int mediaType = selInfo.getSource();
        if (onlySelType == 100) {
            compareAndShowTips(orgSource, mediaType, tipsListener);
            return orgSource == mediaType;
        } else if (onlySelType == 1) {
            compareAndShowTips(mediaType, LibMediaType.PIC.getSource(), tipsListener);
            return mediaType == LibMediaType.PIC.getSource();
        } else if (onlySelType == 2) {
            compareAndShowTips(mediaType, LibMediaType.VIDEO.getSource(), tipsListener);
            return mediaType == LibMediaType.VIDEO.getSource();
        } else {
            compareAndShowTips(orgSource, mediaType, tipsListener);
            return (orgSource == mediaType);
        }
    }

    /**
     * 对比类型，输入文字
     */
    private static void compareAndShowTips(int onlySelType, int mediaType, TipsListener tipsListener) {
        if (tipsListener == null) {
            return;
        }
        if (onlySelType == mediaType) {
            return;
        }
        String message = "";
        if (onlySelType == LibMediaType.PIC.getSource()) {
            message = "图片资源";
        } else if (onlySelType == LibMediaType.VIDEO.getSource()) {
            message = "视频资源";
        } else {
            message = "一种";
        }
        if (tipsListener != null) {
            tipsListener.message("只能选择" + message + "类型的资源");
        }
    }

    /**
     * 数据格式转换
     * */
    /**
     * 数据转化--所有数据对外调用时，都需要通过此方法，转换数据
     * 目前只对文件名进行数据格式比对--后续需要增加头文件头进行对比
     */
    public static LibPicSelMediaInfo trainInfo(Context context, String path) {
        String newPath = LibPicFileUtils.copyFileUsingFileStreams(path, LibPicConstant.getInstance().getCopyPath());
        return trainInfo(context, path, newPath);
    }

    public static LibPicSelMediaInfo trainInfo(Context context, String orgPath, String copyPath) {
        LibPicSelMediaInfo result = new LibPicSelMediaInfo(orgPath, copyPath);
        try {
            if (!TextUtils.isEmpty(copyPath)) {
                LibPicMediaExtraInfo mediaExtraInfo = new LibPicMediaExtraInfo();
                String lower = copyPath.toLowerCase();
                if (lower.contains(".avi") || lower.contains(".mp4") ||
                        lower.contains(".3gp") || lower.contains(".mpeg") ||
                        lower.contains(".rm") || lower.contains(".rmvb") ||
                        lower.contains(".wav")) {
                    result.setResType(LibMediaType.VIDEO.getSource());
                    //视频资源
                    mediaExtraInfo = LibMediaInfoUtils.getVideoSize(context, copyPath);
                } else {
                    result.setResType(LibMediaType.PIC.getSource());
                    mediaExtraInfo = LibMediaInfoUtils.getPicSizeExif(context, copyPath);
                }
                //设置媒体信息
                result.setDuration(mediaExtraInfo.getDuration());
                result.setWidth(mediaExtraInfo.getWidth());
                result.setHeight(mediaExtraInfo.getHeight());
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "error: " + e.getMessage());
        }

        return result;
    }

    //监听
    public interface TipsListener {
        void message(String tips);
    }
}
